package com.baizhi.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.dao.AlbumDao;

import com.baizhi.entity.CmfzAlbum;
import com.baizhi.entity.CmfzAudio;

import com.baizhi.service.CmfzAlbumService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("album")


public class CmfzAlbumController {
    @Autowired
    private CmfzAlbumService cas;

    /**
     * 分页显示所有上师专辑
     *
     */
    @RequestMapping("selByPage")
    public Map<String,Object> selByPage(int page,int limit){
        Map<String,Object> map=new HashMap<>();
        Page<CmfzAlbum> cmfzAlbumPage = cas.selByPage(page, limit);
        for (CmfzAlbum record : cmfzAlbumPage.getRecords()) {
            System.out.println(record);
        }
        map.put("code",0);
        map.put("msg","");
        map.put("data",cmfzAlbumPage.getRecords());


        map.put("count",cmfzAlbumPage.getTotal());
        return map;
    }

    /**
     * 删除一个上师专辑
     * 连同专辑下的所有音频删除
     */
    @RequestMapping("delAlbum")
    public Boolean delAlbum(int id){
        try {
            cas.delete(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }
    /**
     * 添加一个专辑
     */
    @RequestMapping("addAlbum")
    public Boolean addAlbum(CmfzAlbum album){
        try {
            cas.update(album);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 修改一个专辑
     */
    @RequestMapping("updAlbum")
    public Boolean updAlbum(CmfzAlbum album){
        try {
            cas.update(album);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 查看一个专辑可跳转至专辑下的所有音乐，并支持单个音乐删除，播放，添加等功能
     */
    @RequestMapping("catAlbum")
    public List<CmfzAudio> catAlbum(int id){
        List<CmfzAudio> cmfzAudios = cas.catAlbum(id);

        return cmfzAudios;
    }


    //上传
    @RequestMapping("upload")
    public Map upload(MultipartFile file) throws IOException {
        //在文件名的前面添加上uuid避免文件名被覆盖
        String newName= UUID.randomUUID().toString()+ file.getOriginalFilename();

        //把文件复制到这个目录里面。
        file.transferTo(new File("D:/img/"+newName));

        //响应信息。code=0表示上传成功，src是图片上传后新的名字
        Map map=new HashMap();
        map.put("code",0);
        map.put("src",newName);
        return map;
    }

    //所有上师不分页
    @Autowired
    private AlbumDao albumDao;
    @RequestMapping("allGuru")
    public List<CmfzAlbum> allGuru(){
        List<CmfzAlbum> albums = albumDao.selectList(null);
        return albums;
    }

    //将上师所有的数据导出Excel
    @RequestMapping("albumToExcel")
    public String guruToExcel(HttpServletResponse response) throws IOException {
        String s="album.xls";
        response.reset();
        response.setHeader("Content-Disposition","attachment;fileName="+ URLEncoder.encode(s,"utf-8"));
        response.setContentType("application/vnd.ms-excel;charsert:utf-8");
        response.setHeader("Pragma","no-cache");
        response.setHeader("Cacha-Control","no-cache");
        response.setDateHeader("Expires",0);
        List<CmfzAlbum> albums=albumDao.selectList(null);
        for (CmfzAlbum album : albums) {
            System.out.println(album);
        }
        ExportParams exportParams = new ExportParams("标题", "album");
        Workbook sheets = ExcelExportUtil.exportExcel(exportParams, CmfzAlbum.class, albums);
        OutputStream outputStream = response.getOutputStream();
        BufferedOutputStream stream = new BufferedOutputStream(outputStream);
        sheets.write(stream);
        stream.flush();
        stream.close();
        outputStream.close();

        return "success";

    }

    //导入，将Excel文件导入为数据库中的数据
    /**
     * 导入
     *
     */
    @RequestMapping("excelToJava")
    public Map excelToJava(MultipartFile file) throws Exception {
        //流读取文件



        InputStream inputStream = file.getInputStream();
        // HSSFWorkbook sheets = new HSSFWorkbook(inputStream);
        Map<String,Object> map=new HashMap<>();
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(1);
        importParams.setHeadRows(1);
        List<CmfzAlbum> list= ExcelImportUtil.importExcel(inputStream,CmfzAlbum.class,importParams);
        for (CmfzAlbum cmfzAlbum : list) {
            albumDao.insert(cmfzAlbum);
        }
        map.put("code",0);
        return map;

    }
   @PutMapping("test")
    public String test(){
        return null;
   }


}
